﻿using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using Wasion.HHU.Data.Models;
using Wasion.HHU.Data.Repositories;
using SQLite.Net;
using System;
using System.Linq.Expressions;

namespace Wasion.HHU.Data.Repositories
{
    public class VObjectsDbRepository : BaseDbRepository
    {
        public VObjectsDbRepository() : base()
        {

        }
        public VObjectsDbRepository(SQLiteConnection connection)
            : base(connection)
        {
        }

        public Task<List<VObjects>> GetAllList()
        {
            List<VObjects> VObjectss;

            lock (databaseLock)
            {
                VObjectss = DbConnection.Table<VObjects>().
                    ToList();
            }
            return Task.FromResult(VObjectss);
        }

        public Task<List<VObjects>> GetList(Expression<Func<VObjects, bool>> filter)
        {
            List<VObjects> VObjectss;

            lock (databaseLock)
            {
                VObjectss = DbConnection.Table<VObjects>().
                    Where(filter).
                    ToList();
            }
            return Task.FromResult(VObjectss);
        }

        public void Insert(VObjects vObjects)
        {
            lock (databaseLock)
            {
                DbConnection.CreateTable<VObjects>();
                DbConnection.Insert(vObjects);
            }
        }

        public void Update(VObjects vObjects)
        {
            lock (databaseLock)
            {
                DbConnection.Update(vObjects);
            }
        }

        public VObjects Find(object primaryKey)
        {
            VObjects vObjects;

            lock (databaseLock)
            {
                vObjects = DbConnection.Find<VObjects>(primaryKey);
            }
            return vObjects;
        }

        public void Delete(object primaryKey)
        {
            lock (databaseLock)
            {
                DbConnection.Delete<VObjects>(primaryKey);
            }
        }
    }
}
